package com.clippick.history;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.clippick.MainActivity;
import co.kr.clippick.R;
import com.clippick.common.GlobalValue;
import com.clippick.common.ImageLoader;
import com.clippick.database.DrawableManager;
import com.clippick.database.FeedOrderDBAdapter;
import com.clippick.database.HistoryDBAdapter;

public class HistoryAdapter extends BaseAdapter {
	// common
	private Context mContext;
	private Activity activity;

	// controller
	private ImageLoader imageLoader;

	// view
	private LayoutInflater inflater;
	private ListView list;
	private View v;

	// clip data
	private DrawableManager manager;
	private List<Map> result = new ArrayList<Map>();

	public HistoryAdapter(Context context) {
		mContext = context;
		activity = (Activity) mContext;
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(mContext);
		manager = new DrawableManager();
	}

	public void addClipData(List<Map> data) {
		result = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method
		return result.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return result.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ClipHolder h;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.clip_item, parent, false);
			h = new ClipHolder();
			h.layout = (LinearLayout) convertView.findViewById(R.id.layAddView);
			h.starCheck = (CheckBox) convertView.findViewById(R.id.check_star);
			h.image = (ImageView) convertView.findViewById(R.id.image);
			h.date = (TextView) convertView.findViewById(R.id.date);
			h.device = (TextView) convertView.findViewById(R.id.device);
			h.text = (TextView) convertView.findViewById(R.id.content);

			h.layout.setFocusable(false);
			h.layout.setFocusableInTouchMode(false);

			convertView.setTag(h);
		} else {
			h = (ClipHolder) convertView.getTag();
		}
		Map item = result.get(position);

		h.layout.removeAllViews();
		h.layout.setFocusable(false);

		if (item.get(FeedOrderDBAdapter.KEY_DB_TEXT_PATH) != null) {
			String path = item.get(FeedOrderDBAdapter.KEY_DB_TEXT_PATH)
					.toString();
			if (path.contains(GlobalValue.CLIPPICK_IMAGE_ROOT_FOLDER)) {
				// content: image only
				ImageView imageView = new ImageView(mContext);
				imageView.setFocusable(false);
				imageView.setAdjustViewBounds(true);
				imageLoader.showImage(path, imageView, 1);
				h.layout.addView(imageView);
			} else {
				// content: text only
				TextView textView = new TextView(mContext);
				textView.setText(path);
				textView.setTextColor(Color.parseColor("#696969"));
				textView.setFocusable(false);

				// linkfy text: url, address, date
				Linkify.addLinks(textView, Linkify.ALL);
				textView.setLinkTextColor(Color.rgb(0, 134, 171));

				// add view
				h.layout.addView(textView);

				textView.setFocusable(false);
			}
		}
		// date
		h.date.setText(item.get(HistoryDBAdapter.KEY_DB_DATE).toString());

		// device
		h.device.setText(item.get(HistoryDBAdapter.KEY_DB_DEVICE).toString());

		// star
		boolean isKeep = false;
		if (item.get(HistoryDBAdapter.KEY_DB_KEEPBOX).equals("1")) {
			isKeep = true;
		}
		h.starCheck.setChecked(isKeep);

		h.starCheck.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainActivity activity = (MainActivity) mContext;
				activity.setStar(result.get(position));
				scaleUpFadeOutAnimation(v);
			}
		});

		return convertView;
	}

	private void scaleUpFadeOutAnimation(View v) {
		Animation animation = AnimationUtils.loadAnimation(mContext,
				R.anim.scale_to_translate);
		v.startAnimation(animation);
	}

	public class ClipHolder {
		public CheckBox starCheck;
		public LinearLayout layout;
		public TextView date;
		public TextView device;
		public TextView text;
		public ImageView image;
	}

	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
}
